package com.example.bnuzzu.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bnuzzu.common.dto.PageDto;
import com.example.bnuzzu.common.dto.RoommateListDto;
import com.example.bnuzzu.common.lang.Result;
import com.example.bnuzzu.entity.Roommate;
import com.example.bnuzzu.entity.Userimage;
import com.example.bnuzzu.entity.Userinfo;
import com.example.bnuzzu.service.RoommateService;
import com.example.bnuzzu.service.UserimageService;
import com.example.bnuzzu.service.UserinfoService;
import com.example.bnuzzu.util.ShiroUtil;
import org.apache.catalina.User;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author Corn
 */
@RestController
public class RoommateController {
    @Autowired
    RoommateService roommateService;
    @Autowired
    UserinfoService userinfoService;
    @Autowired
    UserimageService userimageService;

    @PostMapping("/FindRoommate/{page}")
    public Result Find(@RequestBody RoommateListDto roommateListDto,@PathVariable(name = "page", required = false) Integer p)
    {
        Page<RoommateListDto> page = new Page<>(p, 6);
        IPage<RoommateListDto> pageData = roommateService.searchPage(page,roommateListDto);
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("pageData",pageData);
        return Result.success(map);
    }

    @PostMapping("/editRoommate")
    @RequiresAuthentication
    public Result Edit(@RequestBody Roommate roommate){

        if(roommate.getUserid() != null){
            Assert.isTrue(roommate.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        roommateService.saveOrUpdate(roommate);
        return Result.success(null);
    }

    @GetMapping("/getRommate/{id}")
    public Result GetInfo(@PathVariable(name = "id") Integer id){
        Roommate roommate = roommateService.getById(id);
        Userinfo userinfo = userinfoService.getById(roommate.getUserid());
        Userimage userimage = userimageService.getById(roommate.getUserid());
        RoommateListDto roommateListDto = new RoommateListDto();
        BeanUtil.copyProperties(userinfo,roommateListDto);
        BeanUtil.copyProperties(roommate,roommateListDto);
        BeanUtil.copyProperties(userimage,roommateListDto);

        return Result.success(roommateListDto);
    }

    @GetMapping("/getRommateInfo/{id}")
    @RequiresAuthentication
    public Result GetRm(@PathVariable(name = "id" ,required = false) String userid){
        if(userid.equals("undefined")){
            return Result.success(null);
        }

        if(userid != null){
            Assert.isTrue(Integer.parseInt(userid) == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }
        QueryWrapper<Roommate> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        Roommate roommate = roommateService.getOne(wrapper);

        return Result.success(roommate);
    }

    @PostMapping("/delRoommate")
    @RequiresAuthentication
    public Result Del(@RequestBody Roommate roommate){
        if(roommate.getUserid() != null){
            Assert.isTrue(roommate.getUserid() == ShiroUtil.getProfile().getUserid().intValue(), "没有权限编辑");
        }else {
            return Result.fail(500,"请重新登录",null);
        }

        roommateService.removeById(roommate);
        return Result.success(null);
    }

}
